package cn.stylefeng.guns.modular.au8;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * @创建人 xiesm
 * @创建时间 2021/8/22
 * @描述
 */

public class Aulucky8Controller {

    public static void main(String[] args) throws IOException, ParseException {
        Aulucky8Controller a = new Aulucky8Controller();
        a.getSummary(null);
    }

    public void getSummary(String num) throws IOException, ParseException {
        String url = "https://api.api68.com/klsf/getHistoryLotteryInfo.do?date=&lotCode=10011";
        //http请求获取比赛详情
        JSONObject jsonObject = sendHttp(String.format(url, num));
        List<Aulucky8> lists = new ArrayList<>();
        if ("0".equals(jsonObject.getString("errorCode"))) {
            JSONArray datas = jsonObject.getJSONObject("result").getJSONArray("data");
            for (int i = 0; i < datas.size(); i++) {
                JSONObject data = datas.getJSONObject(i);
                Aulucky8 aulucky8 = new Aulucky8();
                aulucky8.setNumber(data.getString("preDrawCode").split(","));
                aulucky8.setId(data.getString("preDrawIssue"));
                lists.add(aulucky8);
            }
        }

        CollUtil.sortByProperty(lists, "id");

        ExcelWriter writer = ExcelUtil.getWriter();
        writer.addHeaderAlias("id", "期号");
        writer.addHeaderAlias("number1", "号码1");
        writer.addHeaderAlias("number2", "号码2");
        writer.addHeaderAlias("number3", "号码3");
        writer.addHeaderAlias("number4", "号码4");
        writer.addHeaderAlias("number5", "号码5");
        writer.addHeaderAlias("number6", "号码6");
        writer.addHeaderAlias("number7", "号码7");
        writer.addHeaderAlias("number8", "号码8");
        writer.write(lists);
        writer.flush(new File("D:\\杂七杂八\\1.xls"));
    }

    private JSONObject sendHttp(String url) throws IOException {

        return JSONObject.parseObject(HttpUtil.get(url));
    }
}
